package 题目集.拓扑排序;

import 题目集.dfs.图.ch03_食物链条数;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * 最大食物链计数
 * a -> b，代表a在食物链中被b捕食
 * 给定一个有向无环图，返回
 * 这个图中从最初级动物到最顶级捕食者的食物链有几条
 * 测试链接 : https://www.luogu.com.cn/problem/P4017
 */
public class ch01_食物链条数 {
    /**
     * 暴力思路：
     *      dfs+记忆化搜索:{@link ch03_食物链条数}
     * 拓扑排序思路：
     *      将所有顶级捕食者看作入度为0的点，每次将顶级捕食者的消息推给下级捕食者。直到没有顶级捕食者
     *      最后的结果就是所有的食物链的数量。
     */
    static List<Integer>[] graph;
    static int mod = 80112002;
    static int[] inDegree;
    static int[] msg;
    static int[] queue;
    static int head, tail;

    public static void main(String[] args) {
        Reader sc = new Reader();
        int n = sc.nextInt();
        int m = sc.nextInt();
        graph = new List[n + 1];
        inDegree = new int[n + 1];
        queue = new int[n + 1];
        msg = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            graph[a].add(b);
            inDegree[b]++;
        }
        for (int i = 1; i < inDegree.length; i++) {
            if (inDegree[i] == 0) {
                queue[tail++] = i;
                msg[i] = 1;
            }
        }
        System.out.println(topoSort());
    }

    /**
     * 拓扑排序
     * @return
     */
    public static long topoSort() {
        long res = 0;
        while (head < tail) {
            int cur = queue[head++];
            if (!graph[cur].isEmpty()) {    //不是终点
                for (Integer to : graph[cur]) { //将到自己的路径推送给依赖自己的节点
                    inDegree[to]--;
                    msg[to] = (msg[to] + msg[cur]) % mod;
                    if (inDegree[to] == 0) {
                        queue[tail++] = to;
                    }
                }
            } else {
                res = (res + msg[cur]) % mod;
            }
        }
        return res;
    }

    static class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public void clear() {
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }
}
